/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;
import java.sql.*;
import java.util.Date;
/**
 *
 * @author cpn02
 */
import bean.*;
import java.sql.Statement;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.security.krb5.internal.Ticket;
public class CusModel {
    public static Customer getCustomer(String userName, String passw)
    {
        Customer cus = null;
        Connector cnntor = new Connector();
         String query = "select * from customer where userName='"+userName+"' and passw='"+passw+"' and status ='1'";
     
        try {
                Statement st = cnntor.connect().createStatement();
                ResultSet rs = st.executeQuery(query);
                if(rs.next())
                {
                    int i = rs.getInt("IDCus");
                    String un = rs.getString("userName");
                    String pw = rs.getString("passw");
                    String email = rs.getString("email");
                    String phone = rs.getString("phone");
                    String address = rs.getString("address");
                    cus =  new Customer();
                    cus.setIDCus(i);
                    cus.setAddress(address);
                    cus.setEmail(email);
                    cus.setPassw(pw);
                    cus.setPhone(phone);
                    cus.setUserName(un);
                    cnntor.closeDB();
                    return cus;
                 }
                
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
         cnntor.closeDB();
         return null;
    }
    public static boolean isExitsUserName(String userName)
    {

        Connector cnntor = new Connector();
        String query = "select * from customer where userName='"+userName.trim()+"'";
        try {
                Statement st = cnntor.connect().createStatement();
                ResultSet rs = st.executeQuery(query);
                
                if(rs.next())
                {
                    cnntor.closeDB();
                    return true;
                }
        }catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
         cnntor.closeDB();
         return false;
    }
    public static void Register(String userName,String passw, String email, String phone, String address,int gender)
    {
        Customer cus = null;
        Connector cnntor = new Connector();
        Date d = new Date();
        String stdate = d.getYear()+"-"+d.getMonth()+"-"+d.getDate();
        String query = "insert into customer values(NULL,'"+userName+"','"+passw+"','"+email+"','"+phone+"','"+address+"','"+gender+"','"+stdate+"','1')";
        try {
                Statement st = cnntor.connect().createStatement();
                st.execute(query);

        }catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
         cnntor.closeDB();
    }
   
}